package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.system.service.ISysDictService;
import com.gfxy.modules.items.entity.Cbjnds;
import com.gfxy.modules.items.mapper.CbjndsMapper;
import com.gfxy.modules.items.service.ICbjndsService;
import com.gfxy.modules.items.vo.CbjndsMemberPage;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.jeecg.common.system.vo.DictModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: gfxy_t_cjrh
 * @Author: jeecg-boot
 * @Date:   2023-03-24
 * @Version: V1.0
 */
@Service
public class CbjndsServiceImpl extends AbstractProjectServiceImpl<CbjndsMapper, Cbjnds> implements ICbjndsService {

	private static final Logger logger = LoggerFactory.getLogger(CbjndsServiceImpl.class);

	//todo
	protected int memberCellNum = 3;

	@Autowired
	private ISysDictService sysDictService;

	private Map<String,String> levelIntToNameMap = new HashMap<>();

	private Map<String,String> levelNameToIntMap = new HashMap<>();



	@Override
	public Double getTotalScore(Cbjnds cbjnds) {
		double result = 0.0;
		int level = cbjnds.getLevel();
		if(level == 1){
			result = 150;
		} else if(level == 2){
			result = 50;
		} else if(level == 3){
			result = 30;
		}
		return result;
	}

	@Override
	protected void pass(String id, String isPass) {

	}

	@Override
	protected Cbjnds getOneProject(Row row) {

		this.loadDict();


		String year = ExcelUtil.formatCell(row.getCell(0));

		String name = ExcelUtil.formatCell(row.getCell(1));
		String levelStr = ExcelUtil.formatCell(row.getCell(2));



		Cbjnds cbjnds = new Cbjnds();
		cbjnds.setDate(year);
		cbjnds.setProjectName(name);

		Integer level = Integer.parseInt(this.levelNameToIntMap.get(levelStr));
		cbjnds.setLevel(level);


		cbjnds.setIsPass(0);

		//这个地方不要忘了-算总分
		cbjnds.setTotalScore(this.getTotalScore(cbjnds));
		return cbjnds;
	}

	private void loadDict() {



		this.initDict("CBJNDC_LEVEL", this.levelIntToNameMap, this.levelNameToIntMap);



	}

	private void initDict(String dictName, Map<String,String> intToNameMap, Map<String, String> nameToIntMap){

		if(intToNameMap.isEmpty()|| nameToIntMap.isEmpty()){
			logger.debug("开始查询字典名为[{}] 的字典值", dictName);
			List<DictModel> dictModelList =  sysDictService.queryDictItemsByCode(dictName);

			for (DictModel dictModel : dictModelList) {
				intToNameMap.put(dictModel.getValue(), dictModel.getText());
				nameToIntMap.put(dictModel.getText(), dictModel.getValue());
			}

		}




	}

	@Override
	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}

	@Override
	public IPage<CbjndsMemberPage> selectListPage(String date, Integer pageNo, Integer pageSize) {
		IPage<CbjndsMemberPage> page = new Page<>(pageNo, pageSize);
		return page.setRecords(this.baseMapper.selectCbjndsMemberPageListByYear(date));
	}

	@Override
	public List<CbjndsMemberPage> getCbjndsemberPageListByYear(String date) {
		return this.baseMapper.selectCbjndsMemberPageListByYear(date);
	}
}
